import 'package:fake_handwriting/Util.dart';
import 'package:fake_handwriting/generated/l10n.dart';
import 'package:fake_handwriting/http/http.dart';
import 'package:flutter/material.dart';

class ResetPassword extends StatefulWidget {
  const ResetPassword({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => ResetPasswordPage();
}

class ResetPasswordPage extends State<ResetPassword> {
  final _old_password_controller = TextEditingController();
  final _password_controller = TextEditingController();
  final _password2_controller = TextEditingController();
  @override
  void dispose() {
    super.dispose();
    _old_password_controller.dispose();
    _password_controller.dispose();
    _password2_controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 30),
            child: Column(
              children: [
                TextField(
                  controller: _old_password_controller,
                  obscureText: true,
                  cursorColor: Colors.blue,
                  decoration: InputDecoration(
                      enabledBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      focusedBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      labelText: S.of(context).password,
                      labelStyle: const TextStyle(color: Colors.blue),
                      hintText: S.of(context).inputOldPassword,
                      prefixIcon: const Icon(
                        Icons.password,
                        color: Colors.blue,
                      )),
                ),
                const SizedBox(
                  height: 20,
                ),
                TextField(
                  obscureText: true,
                  controller: _password_controller,
                  cursorColor: Colors.blue,
                  decoration: InputDecoration(
                      enabledBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      focusedBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      labelText: S.of(context).password,
                      labelStyle: const TextStyle(color: Colors.blue),
                      hintText: S.of(context).inputNewPassword,
                      prefixIcon: const Icon(
                        Icons.password_rounded,
                        color: Colors.blue,
                      )),
                ),
                const SizedBox(
                  height: 20,
                ),
                TextField(
                  obscureText: true,
                  controller: _password2_controller,
                  cursorColor: Colors.blue,
                  decoration: InputDecoration(
                      enabledBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      focusedBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      labelText: S.of(context).inputAgain,
                      labelStyle: const TextStyle(color: Colors.blue),
                      hintText: S.of(context).inputNewPasswordAgain,
                      prefixIcon: const Icon(
                        Icons.password_rounded,
                        color: Colors.blue,
                      )),
                ),
                const SizedBox(
                  height: 20,
                ),
                InkWell(
                  onTap: () async {
                    if (_old_password_controller.text.isNotEmpty) {
                      if (_password_controller.text.isNotEmpty) {
                        if (_password2_controller.text.isNotEmpty) {
                          if (_password_controller.text ==
                              _password2_controller.text) {
                            try {
                                var map=await Util.getTokenInfo();
                                if(map==null){
                                  Navigator.pop(context, false);
                                }else{
                                  if (_old_password_controller.text ==map["password"]) {
                                    await Http.reset_password(map["email"], _password_controller.text);
                                    await Util.removeToken();
                                    Util.toast(S.of(context).changePasswordSuccessful);
                                    Navigator.pop(context, true);
                                  } else {
                                    Util.toast(S.of(context).oldPasswordError);
                                  }
                                }
                            } catch (e) {}
                          } else {
                            Util.toast(S.of(context).passwordInconsistent);
                          }
                        } else {
                          Util.toast(S.of(context).inputNewPasswordAgain);
                        }
                      } else {
                        Util.toast(S.of(context).inputNewPassword);
                      }
                    } else {
                      Util.toast(S.of(context).inputOldPassword);
                    }
                  },
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 10),
                    child: Container(
                      padding: const EdgeInsets.symmetric(vertical: 15),
                      width: double.infinity,
                      decoration: const BoxDecoration(
                          color: Colors.blue,
                          borderRadius: BorderRadius.all(Radius.circular(5))),
                      child:Center(
                        child: Text(
                          S.of(context).changePassword,
                          style: const TextStyle(color: Colors.white),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
      appBar: AppBar(
        title:Text(S.of(context).changePassword),
      ),
    );
  }
}
